﻿#region Copyright(C) 2013-2013 NCS Pte. Ltd. All rights reserved.
// ==================================================================================================
// Copyright(C) 2010 NCS Pte. Ltd. All rights reserved.
//
// SYSTEM NAME      : iConnect .Net Application Framework
// COMPONENT ID     : NCS.IConnect.Core.EntLib
// COMPONENT DESC   : Microsoft Enterprise Library Adapter.
//
// CREATED DATE/BY  : 18 Dec 2013 / Jiang Jin Nan
//
// REVISION HISTORY :
// DATE/BY            SR#/CS/PM#/OTHERS       DESCRIPTION OF CHANGE
// ==================================================================================================
#endregion

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.Unity.Utility;
using NCS.IConnect.Core.Configuration;
using NCS.IConnect.Core.IoC;
using NCS.IConnect.Core.SettingSource.Configuration;
using System;
using System.Configuration;

namespace NCS.IConnect.Core.SettingSource
{
    /// <summary>
    /// The <see cref="IConfigurationSource"/> based setting source.
    /// </summary>
    [MapTo(typeof(ISettingSource),1)]
    [ConfigurationElement(typeof(ConfigurationFileSettingSourceData))]
    public class ConfigurationFileSettingSource: ISettingSource
    {
        private string filePath;
        /// <summary>
        /// Gets the configuration source.
        /// </summary>
        /// <value>
        /// The configuration source.
        /// </value>
        public IConfigurationSource ConfigurationSource { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigurationFileSettingSource"/> class.
        /// </summary>
        public ConfigurationFileSettingSource()
        {
            this.ConfigurationSource = new SystemConfigurationSource();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigurationFileSettingSource"/> class.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        public ConfigurationFileSettingSource(string filePath)
        {
            Guard.ArgumentNotNullOrEmpty(filePath, "filePath");
            this.filePath = filePath;
            this.ConfigurationSource = new FileConfigurationSource(filePath);
        }

        /// <summary>
        /// Gets the configuration section based on specified section name.
        /// </summary>
        /// <param name="sectionName">Name of the section.</param>
        /// <returns>
        /// The target configuration section.
        /// </returns>
        public ConfigurationSection GetConfigurationSection(string sectionName)
        {
            Guard.ArgumentNotNullOrEmpty(sectionName, "sectionName");
            return this.ConfigurationSource.GetSection(sectionName);
        }

        /// <summary>
        /// Sets as current settings source.
        /// </summary>
        public void SetAsCurrentSettingsSource()
        {
        }
    }
}
